package com.emspost;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.salt.RandomSaltGenerator;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Genpassword {
    public static void main(String[] args) throws Exception {
//
        String sercet = "admin";
        String salt = "!@#$%^&&**()";


        String password = new Sha256Hash(sercet,salt).toHex();
        System.out.println(password);
//
//        String content = "test中文";

        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("express");
        encryptor.setAlgorithm("PBEWithMD5AndDES");   // optionally set the algorithm
        encryptor.setSaltGenerator(new RandomSaltGenerator());       // for PBE-AES-based algorithms, the IV generator is MANDATORY
        String encryptedText = encryptor.encrypt("Wc123456");
        System.out.println("加密密码:"+encryptedText);
        String plainText = encryptor.decrypt("254nfrfpQy0bxiGv4+oX6KvLzow2ZEU8");
        System.out.println("解密密码:"+plainText);
        String secret_key = IdUtil.simpleUUID();
        System.out.println(secret_key);


        String base64Test01 = Base64.getEncoder().encodeToString("test000001".getBytes(StandardCharsets.UTF_8));
        System.out.println("base64 加密字符串:----" + base64Test01);


        byte[] decoded = Base64.getDecoder().decode(base64Test01);
        String decodedString = new String(decoded, StandardCharsets.UTF_8);
        System.out.println("base64 解密字符串:----" + decodedString);



        // AES 解密（示例使用 ECB 模式，实际应根据安全要求使用 CBC 等安全模式）
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec("79A1El5payelrHdv3MgbDQ==".getBytes(), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encodeString = cipher.doFinal("{\"id\": 11}".getBytes());
        System.out.println(encodeString);
        //转base 64
        String encodeStringBase64 = Base64.getEncoder().encodeToString(encodeString);
        System.out.println("base 64 加密 字符串 " + encodeStringBase64);

        String secretKey = "E6AF2C512C840A34ACAAAF7EC4EEB26A";
        String secretIv = "481505ECF5E6129E";
        AES aes = new AES(Mode.CBC, Padding.ISO10126Padding, secretKey.getBytes(StandardCharsets.UTF_8), secretIv.getBytes(StandardCharsets.UTF_8));
        String decrypt = null;
        System.out.println("加密字符串:" + aes.encryptHex("{\"id\": 11}"));



    }













}
